package br.com.sgdc.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import br.com.sgdc.view.action.ActionAbrirButtonWindowConfiguracaoSistema;
import br.com.sgdc.view.action.ActionConfirmarButtonWindowConfiguracaoSistema;
import br.com.sgdc.view.action.ActionDefinirSenhaCheckBoxWindowConfiguracaoSistema;
import br.com.sgdc.view.action.ActionWindowConfiguracaoSistema;
import br.com.sgdc.view.util.Imagens;

public class WindowConfiguracaoSistema {
	public JDialog window = new JDialog();

	public JLabel caminhoLabel = new JLabel("Pasta Sincronizada:");
	public JLabel usuarioLabel = new JLabel("Usuário:");
	public JLabel senhaLabel = new JLabel("Senha:");
	public JLabel confirmarSenhaLabel = new JLabel("Confirmar Senha:");
	public JLabel perguntaLabel = new JLabel("Pergunta Secreta:");
	public JLabel respostaLabel = new JLabel("Resposta:");

	public JTextField caminhoTextField = new JTextField(15);
	public JTextField usuarioTextField = new JTextField(15);
	public JTextField respostaTextField = new JTextField(15);

	public JButton confirmarButton = new JButton("Confirmar");
	public JButton abrirButton = new JButton("Abrir");

	public JPasswordField senhaPasswordField = new JPasswordField(15);
	public JPasswordField confirmarSenhaPasswordField = new JPasswordField(15);
	public JComboBox<String> perguntaComboBox = new JComboBox<String>();

	public JCheckBox definirSenhaCheckBox = new JCheckBox("Definir Senha", false);

	public WindowConfiguracaoSistema() {
		window.setTitle(".:: SGDC - Configurações ::.");
		window.setLayout(new BorderLayout());
		window.setSize(410, 400);
		window.setLocationRelativeTo(null);
		window.setIconImage(new ImageIcon(getClass().getResource(Imagens.cloud)).getImage());
	}

	public void initComponentesJanela() {
		this.initComponentes();

		window.setLayout(new BorderLayout());
		window.add(BorderLayout.CENTER,constructPrincipalPanel());
		window.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		window.setVisible(true);	

	}

	private void initComponentes() {
		confirmarButton.setIcon(new ImageIcon(getClass().getResource(Imagens.ok)));
		abrirButton.setIcon(new ImageIcon(getClass().getResource(Imagens.open)));

		this.contructAction();
		this.constructCampoComboBox();
	}

	private void constructCampoComboBox() {
		perguntaComboBox.addItem(" ");
		perguntaComboBox.addItem("Qual o nome da sua mãe?");
		perguntaComboBox.addItem("Qual o nome do seu pai?");
		perguntaComboBox.addItem("Qual seu time favorito?");
		perguntaComboBox.addItem("Nome da sua cidade natal?");
	}

	private JPanel constructPrincipalPanel() {
		JPanel panelBorder = new JPanel();
		panelBorder.setLayout(new BorderLayout());
		panelBorder.add(BorderLayout.NORTH,contructPanelCaminho());
		panelBorder.add(BorderLayout.SOUTH,construcutPanelCadastroUsuario());
		panelBorder.add(BorderLayout.CENTER,new JPanel());

		JPanel panelCenter = new JPanel();
		panelCenter.setLayout(new FlowLayout(FlowLayout.CENTER));
		panelCenter.setBorder(BorderFactory.createTitledBorder("Configuração"));
		panelCenter.add(panelBorder);

		return panelCenter;
	}

	private JPanel contructPanelCaminho() {
		JPanel panelGrid = new JPanel();
		panelGrid.setLayout(new GridLayout(3,2));

		panelGrid.add(caminhoLabel);
		panelGrid.add(caminhoTextField);

		panelGrid.add(definirSenhaCheckBox);
		panelGrid.add(abrirButton);

		panelGrid.add(new JPanel());
		panelGrid.add(new JPanel());


		JPanel panelFlow = new JPanel();
		panelFlow.setLayout(new FlowLayout(FlowLayout.CENTER));
		panelFlow.setBorder(BorderFactory.createTitledBorder("Diretório"));
		panelFlow.add(new JLabel());
		panelFlow.add(panelGrid);
		panelFlow.add(new JLabel());

		return panelFlow;
	}

	private JPanel construcutPanelCadastroUsuario() {
		JPanel panelGrid = new JPanel();
		panelGrid.setLayout(new GridLayout(7,2));

		panelGrid.add(new JPanel());
		panelGrid.add(new JPanel());

		panelGrid.add(usuarioLabel);
		panelGrid.add(usuarioTextField);

		panelGrid.add(senhaLabel);
		panelGrid.add(senhaPasswordField);

		panelGrid.add(confirmarSenhaLabel);
		panelGrid.add(confirmarSenhaPasswordField);

		panelGrid.add(perguntaLabel);
		panelGrid.add(perguntaComboBox);

		panelGrid.add(respostaLabel);
		panelGrid.add(respostaTextField);

		panelGrid.add(new JPanel());
		panelGrid.add(new JPanel());

		JPanel panelBorder = new JPanel();
		panelBorder.setLayout(new BorderLayout());
		panelBorder.setBorder(BorderFactory.createTitledBorder("Cadastro Usuario"));
		panelBorder.add(BorderLayout.CENTER,panelGrid);
		panelBorder.add(BorderLayout.WEST,new JPanel());
		panelBorder.add(BorderLayout.EAST,new JPanel());

		JPanel panelButton = new JPanel();
		panelButton.setLayout(new GridLayout(2,2));
		panelButton.add(new JPanel());
		panelButton.add(new JPanel());

		panelButton.add(confirmarButton);
		panelButton.add(new JPanel());

		JPanel principalPanel = new JPanel();
		principalPanel.setLayout(new BorderLayout());
		principalPanel.add(BorderLayout.NORTH,panelBorder);
		principalPanel.add(BorderLayout.SOUTH,panelButton);

		return principalPanel;
	}

	private void contructAction() {
		abrirButton.addActionListener(new ActionAbrirButtonWindowConfiguracaoSistema(this));
		confirmarButton.addActionListener(new ActionConfirmarButtonWindowConfiguracaoSistema(this));
		window.addWindowListener(new ActionWindowConfiguracaoSistema(this));
		definirSenhaCheckBox.addActionListener(new ActionDefinirSenhaCheckBoxWindowConfiguracaoSistema(this));
	}
}
